/**
 * 
 */
package com.dchh.android.suverynew.adapter;

import android.app.Activity;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.dchh.android.suverynew.R;
import com.dhcc.android.base.common.adapter.BaseSwipeAdapter;
import com.dhcc.android.base.common.bean.Item;
import com.dhcc.android.base.common.bean.KeyValueBean;
import com.dhcc.android.base.common.view.SwipeLayout;
import com.dhcc.android.base.utils.StringUtil;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;

/**
 * 带有横向拖出按钮的适配器
 * 
 * @author pengling
 * @date 2015-5-26
 */
public class SlideAdapter extends BaseSwipeAdapter {
	private Activity activity;
	public List<Map<String, String>> mapList;
	private int slideImg;
	private ClickListenerInterface clickListenerInterface;
	public List<Item> itemList;
	public List<KeyValueBean> keyValueList;
	public TextView title_text;
	public String title;

	public void setClicklistener(ClickListenerInterface clickListenerInterface) {
		this.clickListenerInterface = clickListenerInterface;
	}

	public interface ClickListenerInterface {
		public void doOperate(int position);
		public void doItem(int position);
	}

	public SlideAdapter(Activity activity, List<Map<String, String>> mapList,
						int slideImg, List<Item> itemList, List<KeyValueBean> keyValueList, TextView title_text, String title) {
		super();
		this.activity = activity;
		this.mapList = mapList;
		this.slideImg = slideImg;
		this.itemList = itemList;
		this.keyValueList=keyValueList;
		this.title_text=title_text;
		this.title=title;
	}

	public void refreshData(List<Map<String, String>> mapList) {
		this.mapList = mapList;
		notifyDataSetChanged();
	}
	
	public void remove(int position){
	    mapList.remove(position);
	    notifyDataSetChanged();
	}

	@Override
	public int getCount() {
		return mapList.size();
	}

	@Override
	public Object getItem(int position) {
		return mapList.get(position);
	}

	@Override
	public long getItemId(int position) {
		return position;
	}

	@Override
	public int getSwipeLayoutResourceId(int position) {
		return R.id.swipe;
	}

	public int[] user_photo = new int[] { R.mipmap.appicon };// ???

	@Override
	public View generateView( final int position, View convertView,
			ViewGroup parent) {
		final ViewHolder holder;
		if (convertView == null) {
			holder = new ViewHolder();
			convertView = LayoutInflater.from(activity).inflate(
					R.layout.util_slide_list_item, null);
			holder.swipeLayout = (SwipeLayout) convertView
					.findViewById(R.id.swipe);
			holder.infoDelete = (LinearLayout) convertView
					.findViewById(R.id.operate_layout);
			convertView.setTag(holder);
		} else {
			holder = (ViewHolder) convertView.getTag();
		}

		// FrameLayout.LayoutParams layoutParam = new FrameLayout.LayoutParams(
		// Constants.screenWidth / 5,
		// FrameLayout.LayoutParams.MATCH_PARENT);
		// holder.infoDelete.setLayoutParams(layoutParam);

		// 添加删除布局的点击事件
		holder.infoDelete.setOnClickListener(new OnClickListener() {
			@Override
			public void onClick(View v) {
				// System.out.println("Delete");
				System.out.println("pos:"+position);
				int pos=0;
				pos=position;
				if(mapList.size()==2&&position==2){
						System.out.println("position："+position);
						pos=position-1;
				}
				clickListenerInterface.doOperate(pos);
			}
		});
		// 点击整行
		holder.swipeLayout.setOnClickListener(new OnClickListener() {
			@Override
			public void onClick(View arg0) {
				clickListenerInterface.doItem(position);
			}
		});
		return convertView;
	}
	@Override
	public void fillValues(int position, View convertView) {
		Map<String, String> map = mapList.get(position);

		TextView id_text = (TextView) convertView.findViewById(R.id.id);

		// String id=(String) CommenUtil.getFieldValueByName("rowid",
		// entity);//id是固定的，得统一

		id_text.setText(map.get("rowid"));
		List<String> strList = new ArrayList<String>();
		for (Item item : itemList) {
			String feild = item.getFeild();
			String value = map.get(feild);
			if (StringUtil.isEmpty(value)) {
				value="暂无";
			}
//			if(keyValueList!=null&&keyValueList.size()>0){
//				for (KeyValueBean keyValueBean : keyValueList) {
//					String key=keyValueBean.getKey();
//					String selectValue=keyValueBean.getValue();
////					System.out.println("key:"+key+","+selectValue);
//					if(key.equals(value)){
//						value=selectValue;？？
//						break;
//					}
//				}
//			}
//			System.out.println("value:"+value.length());
			if(StringUtil.isNotEmpty(value)&&value.length()>4){//过长，用...代替
				value=value.substring(0,4)+"...";
			}
			strList.add(item.getLabel() + " : " + value);
		}

		if (strList != null && strList.size() != 0) {
			TextView title_text = (TextView) convertView
					.findViewById(R.id.title_text);
			LinearLayout child_layout = (LinearLayout) convertView
					.findViewById(R.id.child_layout);
			title_text.setText(strList.get(0));
			View childView = child_layout.findViewById(R.id.slide_layout);
			if (childView == null) {// 控制不重复加view
				for (int i = 1; i <= strList.size() - 1; i = i + 2) {
					childView = activity.getLayoutInflater().inflate(
							R.layout.util_slide_list_item_childview, null);
					TextView left_text = (TextView) childView
							.findViewById(R.id.left_text);
					TextView right_text = (TextView) childView
							.findViewById(R.id.right_text);
					if (i == strList.size() - 1) {
						left_text.setText(strList.get(i));
					} else {
						left_text.setText(strList.get(i));
						right_text.setText(strList.get(i + 1));
					}
					child_layout.addView(childView);
				}
			} 
		}
		if (slideImg != 0) {// 设置拖出图片
			TextView img = (TextView) convertView.findViewById(R.id.img);
			img.setBackgroundResource(slideImg);
		}

	}

	public class ViewHolder {
		public LinearLayout infoDelete;
		public SwipeLayout swipeLayout;
	}
}
